package edu.gatech.aristotle.random.faultMatrix;

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.HashMap;

import com.sun.corba.se.impl.javax.rmi.CORBA.Util;

import edu.gatech.aristotle.random.util.CONSTANT_FILE_NAME;

import net.util.Debug;
import net.util.File_Handler;

/** Read from Siemens fault_matrix,
 * collect testing information.
 * 0: pass
 * 1: fail
 * @author paul
 *
 */
public class MatrixHandler {
	public static void main( String args[] ){
		MatrixHandler maxtrixHandler	= new MatrixHandler();
		
//		for (int i = 0; i < CONSTANT_FILE_NAME.matrixFileNames.length; i++ ){
//			maxtrixHandler.convertTestingResultFormat( CONSTANT_FILE_NAME.matrixFileNames[i],
//														CONSTANT_FILE_NAME.matrixNewFormatFileNames[i] );
//			
//			Debug.println( CONSTANT_FILE_NAME.matrixFileNames[i] + " done!" );
//		}
		
		//convert space
		maxtrixHandler.convertTestingResultFormat( CONSTANT_FILE_NAME.matrixFileNames[5],
													CONSTANT_FILE_NAME.matrixNewFormatFileNames[5] );
		Debug.println( "DONE!" );
	}
	
	public void convertTestingResultFormat( String sourceFileName, String outputFileName ){
		File_Handler.delete_file( outputFileName );	
				
		MatrixHandler matrixHandler	= new MatrixHandler();
		
		HashMap<Integer, String> testingResultMap = matrixHandler.retriveTestingResult( sourceFileName );
		//Debug.println( "retrive done!" );
		
		matrixHandler.writeNewFormatTestingResult( testingResultMap, outputFileName );
		//Debug.println( "write done!" );
	}
	
	public void writeNewFormatTestingResult( HashMap<Integer, String> testingResultMap,
												String outputFileName )
	{
		
		int totalVersion	= testingResultMap.get(1).length();
		int totalTestUnit	= testingResultMap.size();		
		int[][] faultMatrix	= new int[totalVersion][totalTestUnit];
		
		//Debug.println( "Total version is: " + totalVersion );
					
		for ( int i = 0; i <  totalTestUnit; i++ ){
			String result	= testingResultMap.get( i + 1 );
			//Debug.println( "Write result: " + result );
			for ( int j = 0; j < totalVersion; j++ ){
				faultMatrix[j][i] = Integer.parseInt( Character.toString( result.charAt( j ) ) );
			}			
		}

		try{
			for ( int i = 0; i < totalVersion; i++ ){
				StringBuffer newFormatResultBuffer	= new StringBuffer();
				for ( int j = 0; j < totalTestUnit; j ++ ){
					newFormatResultBuffer.append( faultMatrix[i][j] );
				}
				
				
					//Debug.println( "Append new format: " + newFormatResultBuffer );
					File_Handler.append_to_file( outputFileName, newFormatResultBuffer.toString() );
				}
			
			
			File_Handler.append_to_file( outputFileName, "[Test Unit, Version]=[" + totalTestUnit 
																				+ ", " + totalVersion + "]=" 
																				+ totalTestUnit*totalVersion );
		}catch( Exception e){
			System.err.print( "write new format: " + e );				
		}
	}
	/**
	 * 
	 * @param sourceFileName
	 * @return HashMap<Integer, StringBuffer>: Integer test case number; StringBuffer: version results
	 */
	public HashMap<Integer, String> retriveTestingResult(String sourceFileName){
		
		HashMap<Integer, String> testingResultMap	= new HashMap<Integer, String>(); 
		
		boolean unitTestReached		= false;
		StringBuffer versionResult	= new StringBuffer();
		int unitCaseNumber			= -1;
		try {	
			BufferedReader bufferReader = File_Handler.createBufferReader( sourceFileName );
			
			String aLine;
			
			while ( ( aLine= bufferReader.readLine() ) != null ){
				aLine.trim();
				
				if ( aLine.startsWith( "unitest" )  ){					
					unitCaseNumber	= Integer.parseInt( aLine.substring( 7, aLine.length() - 1) );
					//Debug.println( "Unit test number: " + unitCaseNumber );
					
					if ( !unitTestReached ){
						unitTestReached	= true;						
					}else{
						testingResultMap.put( unitCaseNumber, versionResult.toString() );
						versionResult	= new StringBuffer();
					}
				}else if( aLine.startsWith( "v" ) && unitTestReached ){
					// read a new line
					aLine= bufferReader.readLine();
					aLine.trim();
					
					char result = aLine.charAt( aLine.length() - 1 );
					versionResult.append( result );
					//Debug.println( "versionResult: " + result );
				}
			}				
		
		}catch( Exception e){
			System.err.print( "retriveTestingResult: " + e );
			
		}
		
		return testingResultMap;
	}
}
